import Vue from 'vue'
import Router from 'vue-router'
import MainLayout from '@/views/main'
import SqlAgentRoutes from '@/components/storybook/router/sqlAgent'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/modules/demo/login1'),
    name: 'login',
    meta: {
      title: 'login'
    }
  },
  { path: '/desktop', 
  component: () => import('@/views/modules/mspbots/desktop-app/desktop-index'), 
  name: 'desktop', 
  meta: { title: 'Desktop' } 
},
  { path: '/print', 
  component: () => import('@/components/gadget/crud-form-plus/components/form-view-print'), 
  name: 'print', 
  meta: { title: '打印' } 
},
{ path: '/rowNewPage', 
component: () => import('@/components/gadget/crud-form-plus/components/form-view-page'), 
name: 'rowNewPage', 
meta: { title: 'New Page' } 
},
  {
    path: '/emaillogin',
    component: () => import('@/views/pages/login/index'),
    name: 'emaillogin',
    meta: {
      title: 'emaillogin'
    }
  },
  {
    path: '/2fa',
    component: () => import('@/views/pages/2fa/index'),
    name: '2fa',
    meta: {
      title: '2fa'
    }
  },
  {
    path: '/code',
    component: () => import('@/views/pages/code/index'),
    name: 'code',
    meta: {
      title: 'code'
    }
  },
  {
    path: '/password',
    component: () => import('@/views/pages/password/index'),
    name: 'password',
    meta: {
      title: 'password'
    }
  },
  {
    path: '/loginAsAccount',
    caseSensitive: false,
    component: () => import('@/views/pages/loginAsAccount'),
    name: 'loginAsAccount',
    meta: {
      title: 'loginAsAccount'
    }
  },
  {
    path: '/registAsAccount',
    component: () => import('@/views/pages/registAsAccount'),
    name: 'registAsAccount',
    meta: {
      title: 'registAsAccount1'
    }
  },

  {
    path: '/embedWebsite',
    component: () => import('@/views/pages/embedWebsite'),
    name: 'embedWebsite',
    meta: {
      title: 'embedWebsite'
    }
  },

  {
    path: '/regist',
    component: () => import('@/views/pages/regist'),
    name: 'regist',
    meta: {
      title: 'regist'
    }
  },
  {
    path: '/feedback',
    component: () => import('@/views/pages/feedback'),
    name: 'feedback',
    meta: {
      title: 'feedback'
    }
  },
  {
    path: '/verified',
    component: () => import('@/views/pages/verified'),
    name: 'verified',
    meta: {
      title: 'verified'
    }
  },
  {
    path: '/nt',
    component: () => import('@/views/pages/nt'),
    name: 'nt',
    meta: {
      title: 'nt'
    }
  },
  {
    path: '/nextTicket',
    component: () => import('@/components/gadget/next-ticket-view/next-ticket-view-gadget.vue'),
    name: 'nextTicket',
    meta: {
      title: 'nextTicket'
    }
  },
  {
    path: '/NPS',
    component: () => import('@/views/pages/npssurvey'),
    name: 'npssurvey',
    meta: {
      title: 'npssurvey'
    }
  },
  {
    path: '/userForm',
    component: () => import('@/views/pages/userForm'),
    name: 'userForm',
    meta: {
      title: 'userForm'
    }
  },
  {
    path: '/publicReport',
    component: () => import('@/views/pages/publicReport'),
    name: 'publicReport',
    meta: {
      title: 'publicReport'
    }
  },
  {
    path: '/publicReportPdf',
    component: () => import('@/views/pages/publicReportGrid'),
    name: 'publicReportGrid',
    meta: {
      title: 'publicReportGrid'
    }
  },
  {
    path: '/publicReportPdf',
    component: () => import('@/views/pages/publicReport'),
    name: 'publicReportPdf',
    meta: {
      title: 'publicReportPdf'
    }
  },
  {
    path: '/ntreport',
    component: () => import('@/views/pages/ntreport'),
    name: 'ntreport',
    meta: {
      title: 'ntreport'
    }
  },
  // {
  //   path: '/teams/start',
  //   component: () => import('@/views/pages/TeamsStart'),
  //   name: 'start',
  //   meta: {
  //     title: 'start'
  //   }
  // },
  // {
  //   path: '/teams/result',
  //   component: () => import('@/views/pages/TeamsResult'),
  //   name: 'result',
  //   meta: {
  //     title: 'result'
  //   }
  // },
  {
    path: '/Attendance',
    caseSensitive: false,
    name: 'Attendance',
    meta: {
      title: 'Attendance'
    },
    redirect: {
      path: '/Apps?appId=1464133492681084928'
    }
  },
  {
    path: '/present',
    component: () => import('@/views/modules/mspbots/reports/present-layout'),
    name: 'present',
    meta: {
      title: 'Present'
    }
  },
  {
    path: '/portal',
    component: () => import('@/views/modules/mspbots/client-portal/portal'),
    name: 'portal',
    meta: {
      title: 'portal'
    }
  },
  {
    path: '/portalDashboard',
    component: () =>
      import('@/views/modules/mspbots/client-portal/portal-layout'),
    name: 'portalDashboard',
    meta: {
      title: 'portalDashboard'
    }
  },
  {
    path: '/portalPageDashboard',
    component: () =>
      import('@/views/modules/mspbots/client-portal/portal-layout-page'),
    name: 'portalPageDashboard',
    meta: {
      title: 'portalPageDashboard'
    }
  },
  {
    path: '/portal-404',
    component: () =>
      import('@/views/modules/mspbots/client-portal/components/portal-404'),
    name: 'portal-404',
    meta: {
      title: '404'
    }
  },
  {
    path: '/applyEmailGroup',
    component: () =>
      import('@/views/modules/mspbots/peerGroup/applyEmailGroup.vue'),
    name: 'applyEmailGroup',
    meta: {
      title: 'Apply Group'
    }
  },
  {
    path: '/callPhone',
    component: () => import('@/views/pages/callPhone'),
    name: 'callPhone',
    meta: {
      title: 'Call Phone'
    }
  }
]

let hostnameArr = ['portal.mspbots.ai', 'portalint.mspbots.ai']
export const asyncRoutes = (routes = []) => [
  {
    path: '/',
    component: MainLayout,
    name: 'main-dynamic-menu',
    redirect: {
      name: hostnameArr.includes(window.location.hostname) ? 'portal' : 'home'
    },
    children: [
      {
        path: '/ai',
        component: () => import('@/views/modules/ai/index.vue'),
        name: 'AI',
        meta: {
          isTab: true,
          title:'AI',
          menuId: 'ai'
        }
      },
      {
        path: '/home',
        component: () =>
          import('@/views/modules/mspbots/reports/report-layout.vue'),
        name: 'home',
        meta: {
          title: 'home',
          isTab: true
        }
      },
      {
        path: '/paywallBuy',
        component: () =>
          import('@/views/modules/mspbots/payWallManage/paywallBuy'),
        name: 'paywallBuy',
        meta: {
          isTab: true,
          title: 'Customize your purchase plan'
        }
      },
      {
        path: '/paywall-log',
        component: () =>
          import('@/views/modules/mspbots/payWallManage/paywall-log/paywall-log'),
        name: 'paywall-log',
        meta: {
          isTab: true,
          title: 'Paywall log'
        }
      },
      {
        path: '/dashboard-:afterId(.*)',
        component: () =>
          import('@/views/modules/mspbots/reports/report-layout.vue'),
        name: 'DashboardDetailLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/report-:afterId(.*)',
        component: () =>
          import('@/views/modules/report/layout/report-layout.vue'),
        name: 'ReportDetailLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/NextTicketPriorityRules-:afterId(.*)',
        component: () =>
          import('@/views/modules/mspbots/next-ticket-setting/index.vue'),
        name: 'NextTicketPriorityRulesLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/UserFilterRule-:afterId(.*)',
        component: () =>
          import(
            '@/views/modules/mspbots/user-filter&rule/user-filter&rule.vue'
          ),
        name: 'UserFilterRuleLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/ClientWatchList-:afterId(.*)',
        component: () =>
          import(
            '@/views/modules/mspbots/next-ticket-setting/company-watch/company-watch.vue'
          ),
        name: 'ClientWatchListLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/scorecard-:afterId(.*)',
        component: () =>
          import('@/views/modules/mspbots/reports/scorecard-layout.vue'),
        name: 'ScorecardDetailLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/bot-:afterId(.*)',
        component: () => import('@/views/modules/mspbots/dashboard/index.vue'),
        name: 'BotDetailLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/dataset-:afterId(.*)',
        component: () =>
          import('@/views/modules/mspbots/dataset/dataset-view.vue'),
        name: 'DatasetDetailLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/form-:afterId(.*)',
        component: () =>
          import('@/views/modules/mspbots/FormList/FormData/index.vue'),
        name: 'FormDetailLayout',
        meta: {
          isTab: true
        }
      },
      {
        path: '/sys-dict-type__:afterId(.*)',
        component: () => import('@/views/modules/sys/dict-data.vue'),
        name: 'DictManagementType',
        meta: {
          isTab: true
        }
      },
      {
        path: '/404',
        component: () => import('@/views/pages/404'),
        name: '404',
        meta: {
          title: '404 not find'
        },
        beforeEnter(to, from, next) {
          if (/__.*/.test(to.href)) {
            return next(to.redirectedFrom.replace(/__.*/, ''))
          }
          next()
        }
      },

      ...routes
    ]
  },
  {
    path: '/page-404',
    component: () => import('@/views/pages/404'),
    name: 'page-404',
    meta: {
      title: '404 not find'
    },
    beforeEnter(to, from, next) {
      if (/__.*/.test(to.href)) {
        return next(to.redirectedFrom.replace(/__.*/, ''))
      }
      next()
    }
  }
]

export function addDynamicRoute(routeParams, router, noJump) {
  var routeName = routeParams.routeName
  var dynamicRoute = window.SITE_CONFIG['dynamicRoutes'].filter(
    item => item.name === routeName
  )[0]
  if (dynamicRoute) {
    return router.push({ name: routeName, params: routeParams.params })
  }
  dynamicRoute = {
    path: routeName,
    // component: () => import(`@/views/modules/${routeParams.path}`),
    component: () => import(/* webpackChunkName: "[index]" */ `@/views/modules/${routeParams.path}`),
    // component: () =>
    //   Promise.resolve(require(`@/views/modules/${routeParams.path}`).default),
    name: routeName,
    meta: {
      ...window.SITE_CONFIG['contentTabDefault'],
      menuId: routeParams.menuId,
      title: `${routeParams.title}`
    }
  }
  router.addRoutes([
    {
      ...constantRoutes,
      name: `main-dynamic__${dynamicRoute.name}`,
      children: [dynamicRoute]
    }
  ])
  window.SITE_CONFIG['dynamicRoutes'].push(dynamicRoute)
  if (!noJump) {
    router.push({
      name: dynamicRoute.name,
      params: routeParams.params,
      query: routeParams.query
    })
  }
}
const createRouter = () =>
  new Router({
    mode: 'history',
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRoutes.concat(asyncRoutes()).concat(SqlAgentRoutes())
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
